package com.sanpu.kxe.module.dialog;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseDialog;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.module.specialunit.SimplifySpanBuild;
import com.sanpu.kxe.module.specialunit.unit.SpecialTextUnit;

import java.math.BigDecimal;

import butterknife.ButterKnife;

/**
 * Description：红包对话框
 * Created by 薛勇军 on 2018/7/9.
 */
public class PackageDialog extends BaseDialog<PackageDialog> {
    private ImageView vOpen;
    private RoundTextView vOk;
    private LinearLayout vContentContainer;
    private CharSequence content;
    private TextView vAmount;
    private String title;
    private String des;
    private TextView vDes;
    private ImageView vBg;
    private UserCenterOutput.ActReb actRebBean;

    public PackageDialog content(CharSequence content) {
        this.content = content;
        return this;
    }

    public PackageDialog(Context context) {
        super(context);
    }

    public PackageDialog setData(UserCenterOutput.ActReb actRebBean) {
        this.actRebBean = actRebBean;
        return this;
    }

    @Override
    public View onCreateView() {
        showAnim(new BaseAnimatorSet() {
            @Override
            public void setAnimation(View view) {
            }
        });

        View inflate = View.inflate(getContext(), R.layout.dialog_back_package, null);
        vContentContainer = ButterKnife.findById(inflate, R.id.des_layout);
        vAmount = ButterKnife.findById(inflate, R.id.amount);
        vDes = ButterKnife.findById(inflate, R.id.des);
        vOpen = ButterKnife.findById(inflate, R.id.open);
        vOk = ButterKnife.findById(inflate, R.id.ok);
        vBg = ButterKnife.findById(inflate, R.id.bg);

        try {
            SimplifySpanBuild desBuild = new SimplifySpanBuild(mContext, vAmount);
            if (actRebBean.getAmt() != null && actRebBean.getAmt().compareTo(BigDecimal.ZERO) == 1 && actRebBean.getBp() > 0) {
                desBuild.appendSpecialUnit(new SpecialTextUnit("获得", Color.parseColor("#ffffff")).setTextSize(12));
                desBuild.appendSpecialUnit(new SpecialTextUnit(actRebBean.getAmt().toPlainString(), Color.parseColor("#F4CB55")).setTextSize(12));
                desBuild.appendSpecialUnit(new SpecialTextUnit("元，", Color.parseColor("#ffffff")).setTextSize(12));

//                desBuild.appendSpecialUnit(new SpecialTextUnit("获得", Color.parseColor("#ffffff")).setTextSize(12));
                desBuild.appendSpecialUnit(new SpecialTextUnit(actRebBean.getBp() + "", Color.parseColor("#F4CB55")).setTextSize(12));
                desBuild.appendSpecialUnit(new SpecialTextUnit("积分", Color.parseColor("#ffffff")).setTextSize(12));
            } else if (actRebBean.getBp() > 0) {
                desBuild.appendSpecialUnit(new SpecialTextUnit("获得", Color.parseColor("#ffffff")).setTextSize(12));
                desBuild.appendSpecialUnit(new SpecialTextUnit(actRebBean.getBp() + "", Color.parseColor("#F4CB55")).setTextSize(20));
                desBuild.appendSpecialUnit(new SpecialTextUnit("积分", Color.parseColor("#ffffff")).setTextSize(12));
            } else {
                desBuild.appendSpecialUnit(new SpecialTextUnit("获得", Color.parseColor("#ffffff")).setTextSize(12));
                desBuild.appendSpecialUnit(new SpecialTextUnit(actRebBean.getAmt().toPlainString(), Color.parseColor("#F4CB55")).setTextSize(20));
                desBuild.appendSpecialUnit(new SpecialTextUnit("元", Color.parseColor("#ffffff")).setTextSize(12));
            }
            vAmount.setText(desBuild.build());
            vDes.setText("奖励已存入账户，金额可立即提现");
        } catch (Exception e) {
            e.printStackTrace();
            dismiss();
        }
        inflate.setPadding(dp2px(15), dp2px(15), dp2px(15), dp2px(15));
        return inflate;
    }

    @Override
    public void setUiBeforShow() {
        vOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        vOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2018/7/9 开红包接口
//                new UserClient().updateHpsAwRead(mContext);

                ObjectAnimator animator = ObjectAnimator.ofFloat(vOpen, "rotationY", 0, 180, 360);
                animator.setDuration(1500);
                animator.setRepeatCount(1);
                animator.setRepeatMode(ValueAnimator.RESTART);
                animator.start();
                animator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        vBg.setBackgroundResource(R.drawable.img_package_open);
                        vContentContainer.setVisibility(View.VISIBLE);
                        vOk.setVisibility(View.VISIBLE);
                        vOpen.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
            }
        });
    }

    public PackageDialog title(String title) {
        this.title = title;
        return this;
    }
}